package com.bdqn.servlet;

import com.bdqn.pojo.Supplier;
import com.bdqn.service.SupplierService;
import com.bdqn.service.SupplierServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 系统登录Servlet
 *
 * @author LILIBO
 * @since 2021-09-15
 */
@WebServlet("/supplier")
public class SupplierServlet extends HttpServlet {

    private SupplierService supplierService = new SupplierServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String opr = request.getParameter("opr");

        switch (opr) {
            case "list":
                // 从页面获取参数
                String querySupCode = request.getParameter("querySupCode");
                String querySupName = request.getParameter("querySupName");
                String _pageIndex = request.getParameter("pageIndex");
                // 构造一个参数对象
                Supplier supplier = new Supplier();
                supplier.setSupCode(querySupCode);
                supplier.setSupName(querySupName);
                Integer pageIndex = _pageIndex == null ? 1 : Integer.parseInt(_pageIndex);
                Integer pageSize = 10;
                // 调用Service层获取数据
                List<Supplier> supplierList = supplierService.selectPageList(supplier, pageIndex, pageSize);
                // 将结果放入Request作用域
                request.setAttribute("supplierList", supplierList);
                // 跳转页面（转发）
                request.getRequestDispatcher("jsp/supplier/list.jsp").forward(request, response);
                break;
            case "logout":
                request.getSession().removeAttribute("USER_SESSION");
                response.sendRedirect("jsp/login.jsp");
                break;
        }
    }

}
